<template>
    <div class="layout-route">
        <!-- <div class="header">
            <el-button icon="el-icon-arrow-left" @click="cancel">返回</el-button> <span class="header-title">{{ title }}</span>
        </div> -->
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline>
                <el-form-item label="商品sku" prop="seller_sku">
                        <el-input v-model.trim="queryParams.seller_sku" placeholder="请输入商品sku" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item label="商品spu" prop="spu">
                        <el-input v-model.trim="queryParams.spu" placeholder="请输入商品spu" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item label="货架位" prop="warehouse_position">
                        <el-input v-model.trim="queryParams.warehouse_position" placeholder="请输入货架位" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                <el-form-item label="创建时间" prop="create_time">
                    <el-date-picker v-model="queryParams.create_time" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="更新时间" prop="update_time">
                    <el-date-picker v-model="queryParams.update_time" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <el-row :gutter="8">
            <el-col style="width: 20%">
                <el-card shadow="hover" class="text-large chart-card">
                    <div class="chart-card-header text-mini" slot="header">在途数量总计</div>
                    <div class="chart-card-content">
                        <span class="content-title">{{ total_on_way_quantity }} </span>
                    </div>
                </el-card>
            </el-col>
            <el-col style="width: 20%">
                <el-card shadow="hover" class="text-large chart-card">
                    <div class="chart-card-header text-mini" slot="header">未发数量总计</div>
                    <div class="chart-card-content">
                        <span class="content-title">{{ total_not_send_quantity }} </span>
                    </div>
                </el-card>
            </el-col>
            <el-col style="width: 20%">
                <el-card shadow="hover" class="text-large chart-card">
                    <div class="chart-card-header text-mini" slot="header">占用库存</div>
                    <div class="chart-card-content">
                        <span class="content-title">{{ total_using_quantity }} </span>
                    </div>
                </el-card>
            </el-col>
            <el-col style="width: 20%">
                <el-card shadow="hover" class="text-large chart-card">
                    <div class="chart-card-header text-mini" slot="header">可用库存</div>
                    <div class="chart-card-content">
                        <span class="content-title">{{ total_inventory_quantity }} </span>
                    </div>
                </el-card>
            </el-col>
            <el-col style="width: 20%">
                <el-card shadow="hover" class="text-large chart-card">
                    <div class="chart-card-header text-mini" slot="header">库存总数量总计</div>
                    <div class="chart-card-content">
                        <span class="content-title">{{ total_all_inventory_quantity }} </span>
                    </div>
                </el-card>
            </el-col>
        </el-row>
        <div class="card">
            <el-row :gutter="10" class="mb8" type="flex">
                <el-col :span="1.5">
                    <upload-button :action="action" @complete="getList"></upload-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" :loading="download" :disabled="download" @click="handleExport">导出</el-button>
                </el-col>
                <el-col :span="20">
                    <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" placement="top" @pagination="getList"></pagination>
                </el-col>
            </el-row>
            <el-table v-loading="loading" :data="tableData" border script @sort-change="handleSortChange">
                <el-table-column label="商品SKU" prop="seller_sku" min-width="160" show-overflow-tooltip>
                    <template slot-scope="scope">
                        <span>{{ scope.row.seller_sku }}</span>
                        <el-tooltip :content="scope.row.remark" placement="top" v-if="scope.row.remark">
                            <el-tag type="primary" size="mini" style="margin-left: 8px">备</el-tag>
                        </el-tooltip>
                    </template>
                </el-table-column>
                <el-table-column label="图片" min-width="90">
                    <template slot-scope="scope">
                        <ImageUploadImg v-model="scope.row.product_images" :readOnly="true"></ImageUploadImg>
                    </template>
                </el-table-column>
                <el-table-column label="货架位" prop="warehouse_position" min-width="100" show-overflow-tooltip>
                    <template slot-scope="scope">
                        <el-button type="text" @click="showChangePositionDialog(scope.row)" icon="el-icon-edit">{{ scope.row.warehouse_position }}</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="库存情况" align="center">
                    <el-table-column label="安全" prop="safe_quantity" min-width="100" sortable="custom" :sort-orders="['descending', 'ascending']"></el-table-column>
                    <el-table-column label="在途" prop="on_way_quantity" min-width="100" sortable="custom" :sort-orders="['descending', 'ascending']"></el-table-column>
                    <el-table-column label="未发" prop="not_send_quantity" min-width="100" sortable="custom" :sort-orders="['descending', 'ascending']"></el-table-column>
                    <el-table-column label="占用" prop="using_quantity" min-width="100" sortable="custom" :sort-orders="['descending', 'ascending']"></el-table-column>
                    <el-table-column label="可用" prop="inventory_quantity" min-width="100" sortable="custom" :sort-orders="['descending', 'ascending']"></el-table-column>
                    <el-table-column label="总量" prop="all_inventory_quantity" min-width="100" sortable="custom" :sort-orders="['descending', 'ascending']"></el-table-column>
                </el-table-column>
                <el-table-column label="创建时间" prop="create_time" min-width="160" show-overflow-tooltip></el-table-column>
                <el-table-column label="更新时间" prop="update_time" min-width="160" show-overflow-tooltip></el-table-column>
                <el-table-column label="操作" min-width="140" fixed="right">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="showDetailDialog(scope.row)">编辑</el-button>
                        <el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            
        </div>

        <SpuinfoDetailDialog v-model="detailDialog.visible" v-if="detailDialog.visible" :params="detailDialog.data" @success="handleQuery"></SpuinfoDetailDialog>
        <ChangePositionDialog v-model="changePositionDialog.visible" v-if="changePositionDialog.visible" :params="changePositionDialog.data" @success="handleQuery"></ChangePositionDialog>
        <!-- </el-dialog> -->
    </div>
</template>

<script>
import { exportFile } from '@/utils/index'
import { getList, detailedListInfoExport, detailedListInfoDelete } from '@/api/warehouse'
import SpuinfoDetailDialog from './SpuinfoDetailDialog'
import ChangePositionDialog from './ChagePositionDialog'
import { parseTime } from '@/utils/quickadmin'
export default {
    name: 'DetailDialogspuinfo',
    components: { SpuinfoDetailDialog, ChangePositionDialog },
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
    },
    data() {
        return {
            action: process.env.VUE_APP_BASE_API + '/warehouse/DetailedListInfo/import',
            showSearch: true,
            // 查询参数
            queryParams: {
                spu: undefined,
                seller_sku: undefined,
                warehouse_position: undefined,
                create_time: [],
                update_time: [],
            },
            loading: false,
            download: false,
            save: false,
            total_inventory_quantity: 0,
            total_not_send_quantity: 0,
            total_using_quantity: 0,
            total_on_way_quantity: 0,
            total_all_inventory_quantity: 0,
            // 表格数据
            tableData: [],
            pageInfo: {
                page: 1,
                limit: 10,
            },
            total: 0,
            rules: {},
            dict: {},
            detailDialog: {
                visible: false,
                data: {},
            },
            changePositionDialog: {
                visible: false,
                data: {},
            },
        }
    },
    computed: {
        title() {
            return `仓库清单sku详情`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },
    watch: {
        params: {
            handler(val) {
                if (val.id) {
                    this.getList()
                }
            },
            immediate: true,
            deep: true,
        },
    },
    mounted() {
        this.getList()
    },
    methods: {
        // 删除按钮操作
        handleDelete(row) {
            this.$confirm('是否确认删除?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
            .then(async () => {
                await detailedListInfoDelete({ id: row.id })
                this.getList()
                this.msgSuccess('删除成功')
            })
            .catch(() => {})
        },
        resetSort() {
            this.queryParams.safe_quantity = undefined
            this.queryParams.on_way_quantity = undefined
            this.queryParams.not_send_quantity = undefined
            this.queryParams.using_quantity = undefined
            this.queryParams.inventory_quantity = undefined
            this.queryParams.all_inventory_quantity = undefined
        },
        handleSortChange({ column, prop, order }) {
            this.resetSort()
            this.queryParams[prop] = order === 'descending' ? 'desc' : order === 'ascending' ? 'asc' : undefined
            this.getList()
        },
        handleExport() {
            this.download = true
            const qyparams = {
                ...this.pageInfo,
                spu: this.params.spu,
            }
            detailedListInfoExport(qyparams)
                .then(res => {
                    exportFile(res, () => {
                        this.download = false
                    })
                })
                .catch(err => {
                    this.download = false
                })
        },
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },
        getList() {
            this.loading = true
            // const qyparams = { ...this.pageInfo, ...this.queryParams, spu: this.params.spu }
            const qyparams = { ...this.pageInfo, ...this.queryParams }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }
            if (qyparams.update_time?.length == 2) {
                qyparams.update_time = [parseTime(qyparams.update_time[0], 'y-m-d'), parseTime(qyparams.update_time[1], 'y-m-d')]
            }
            getList(qyparams)
                .then(res => {
                    this.tableData = res.data.list
                    this.total = res.data.total
                    this.total_inventory_quantity = res.data?.inventory_quantity_total ?? 0
                    this.total_not_send_quantity = res.data?.not_send_quantity_total ?? 0
                    this.total_using_quantity = res.data?.using_quantity_total ?? 0
                    this.total_on_way_quantity = res.data?.on_way_quantity_total ?? 0
                    this.total_all_inventory_quantity = res.data?.all_inventory_quantity_total ?? 0
                })
                .finally(() => {
                    this.loading = false
                })
        },
        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
        showDetailDialog(row) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
        },
        showChangePositionDialog(row) {
            this.changePositionDialog.visible = true
            this.changePositionDialog.data = row
        },
        deleteItem(row) {
            this.$confirm('确认要删除该供应商吗?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                supListDelete({ id: row.id }).then(res => {
                    this.$message.success(res.msg)
                    this.getList()
                })
            })
        },
        cancel() {
            // this.dialogVisible = false
            this.$emit('cancel')
        },
    },
}
</script>

<style>
.form-class.el-form .el-form-item {
    width: 100%;
    margin-right: 0;
}
.form-class.el-form .el-form-item .el-input {
    width: 400px;
}
.header {
    padding-bottom: 8px;
    border-bottom: 1px solid #f5f5f5;
    margin-bottom: 16px;
}
.header .header-title {
    margin-left: 16px;
    font-size: 16px;
}
</style>
<style lang="scss" scoped>
/* 统计卡片 */
::v-deep .chart-card {
    color: #606266;
    margin: 10px 0;
    .el-card__header,
    .el-card__body {
        padding-left: 32px;
    }
}
.chart-card-num {
    font-size: 30px;
}

.chart-card-content {
    height: 30px;
    .content-title {
        font-weight: bold;
        font-size: 20px;
        color: #303133;
    }
}
</style>
